﻿/*
SINACOR - BMF&Bovespa
Arquivo: GeracaoIntegracaoContabil.cs
Criado em: 28-mai-2010 10:34:37
Arquiteto Responsável: p-wgeneroso
*/
using System;
using System.Collections.Generic;
using System.Text;
using Sinacor.Infra.Common;
using Sinacor.Infra.UI.Common;
using Sinacor.Infra.Common.Exceptions;
using System.Linq;
using System.Collections;


namespace Sinacor.UI.Bovespa.Contabil.Presenter 
{
    public class GeracaoIntegracaoContabil : BasePresenter<IGeracaoIntegracaoContabil> 
	{
		#region Atributos
		private Communication.Calendario _communicationCalendario;
		private Communication.IntegracaoContabil _communicationIntegracaoContabil;
		private Model.Parameter.FiltroIntegracaoContabil _filtro;
		private ModelCollection<Model.Calendario> _listaCalendarios;
		#endregion

		#region Propriedades
		public Model.Parameter.FiltroIntegracaoContabil Filtro
		{
			get { return _filtro; }
			set { _filtro = value; }
		}

		public ModelCollection<Model.Calendario> ListaCalendarios
		{
			get { return _listaCalendarios; }
			set { _listaCalendarios = value; }
		}
		#endregion

        #region Construtor

        /// <summary>
        /// Construtor padrão
        /// </summary>
        public GeracaoIntegracaoContabil(IGeracaoIntegracaoContabil view)
			: base(view) 
        {
            this._communicationCalendario = new Communication.Calendario(this.Session);
            this._communicationIntegracaoContabil = new Communication.IntegracaoContabil(this.Session);
        }

        #endregion

        #region Métodos

        /// <summary>
        /// Gera integração contabil
        /// </summary>
        public void GerarIntegracaoContabil()
		{
            try
            {
                this._communicationIntegracaoContabil.GerarIntegracaoContabil(this._filtro);
                View.InformarSucessoOperacao();
            }
            catch (Exception ex)
            {
                this.HandleException(ex);
            }
		}

        /// <summary>
        /// Inicializa tela
        /// </summary>
		public void Inicializar()
		{
            ModelCollection<Model.Calendario> colecaoCalendario = null;

            try
            {
                colecaoCalendario = _communicationCalendario.ConsultarCalendario();

                if (colecaoCalendario != null)
                {

                    var resultado =
                            from col in colecaoCalendario
                            orderby col.DataPregao descending
                            select col;

                    this._listaCalendarios = new ModelCollection<Model.Calendario>(resultado);
                }

                View.SelecionarUltimaDataCalendario();
                View.AtualizarCalendario();
            }
            catch (Exception ex)
            {
                this.HandleException(ex);
            }
		}

		#endregion
	}

}